/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package cn.icanci.order.factory;

import cn.icanci.basic.exception.ApplicationException;
import cn.icanci.order.dic.OrderStatus;
import cn.icanci.order.entity.OrderEntity;
import cn.icanci.order.module.OrderInfo;
import cn.icanci.order.repository.OrderInfoRepository;
import cn.icanci.order.spi.dto.CouponDTO;
import cn.icanci.order.spi.dto.TicketDTO;
import cn.icanci.order.spi.dto.UserInfoDTO;
import cn.icanci.order.utils.GenerateOrderSerialNo;

import java.util.Date;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

/**
 * @author icanci
 * @date Created in 2021/05/04 15:55
 * @version 1.0
 */
@Component
public class OrderInfoFactory {
    @Resource
    OrderInfoRepository orderInfoRepository;

    /**
     * 构建实体 存在优惠券
     * @param userInfoDTO 用户信息
     * @param ticketDTO
     * @param couponDTO
     * @return
     */
    public OrderEntity create(UserInfoDTO userInfoDTO, TicketDTO ticketDTO, CouponDTO couponDTO) {
        OrderEntity orderEntity = new OrderEntity(orderInfoRepository);
        BeanUtils.copyProperties(userInfoDTO, orderEntity);
        BeanUtils.copyProperties(ticketDTO, orderEntity);
        BeanUtils.copyProperties(couponDTO, orderEntity);
        orderEntity.setTicketId(ticketDTO.getId());
        orderEntity.setUserId(userInfoDTO.getId());
        orderEntity.setCreateTime(new Date());
        orderEntity.setUpdateTime(new Date());
        orderEntity.setOrderStatus(OrderStatus.ORDER_NEW);
        orderEntity.setOrderSerialNo(GenerateOrderSerialNo.generate(orderEntity));
        return orderEntity;
    }

    /**
     * 构建实体 不存在优惠券
     * @param userInfoDTO
     * @param ticketDTO
     * @return
     */
    public OrderEntity create(UserInfoDTO userInfoDTO, TicketDTO ticketDTO) {
        OrderEntity orderEntity = new OrderEntity(orderInfoRepository);
        BeanUtils.copyProperties(userInfoDTO, orderEntity);
        BeanUtils.copyProperties(ticketDTO, orderEntity);
        orderEntity.setTicketId(ticketDTO.getId());
        orderEntity.setUserId(userInfoDTO.getId());
        orderEntity.setCreateTime(new Date());
        orderEntity.setUpdateTime(new Date());
        orderEntity.setOrderStatus(OrderStatus.ORDER_NEW);
        orderEntity.setOrderSerialNo(GenerateOrderSerialNo.generate(orderEntity));
        return orderEntity;
    }

    /**
     * 构建实体 存在优惠券
     * @return
     */
    public OrderEntity create(Long orderId) {
        OrderEntity orderEntity = new OrderEntity(orderInfoRepository);
        OrderInfo orderInfo = orderInfoRepository.queryOneRowByOrderId(orderId);
        if (null == orderInfo) {
            throw new ApplicationException(20001, "订单不存在");
        }
        BeanUtils.copyProperties(orderInfo, orderEntity);
        return orderEntity;
    }
}
